<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class c_setup extends CI_Controller {

	function __construct(){
		parent::__construct();	
		checkLogin();
		$this->load->model('m_setup');
	}
	
	function index(){
		$task = $this->input->post("task");
		switch($task){
			case 'UPDATE' : 
				$this->update(); 
				break;
			case 'SAVE' : 
				$this->save(); 
				break;
			case 'DELETE' : 
				$this->delete(); 
				break;
			case 'GET_PARAM' : 
				$this->get_param(); 
				break;
			case 'LIST_PIC' : 
				$this->list_pic(); 
				break;
			case 'LIST_OWNER' : 
				$this->list_owner(); 
				break;
			case 'UBAH_PASSWORD' : 
				$this->ubah_password(); 
				break;
			default : 
			$this->load->view('v_setup',null);
		}
	}

	function get_param(){
		$param = $this->input->post("param");
		$data = $this->m_setup->get_param($param);
		echo json_encode($data);
	}
	
	function list_pic(){
		$page = $this->input->post('page');
		$rows = $this->input->post('rows');
		$id = $this->input->post('id');
		$option = $this->input->post('option');
		$pic_name = $this->input->post('pic_name');
		$data = $this->m_setup->list_pic($option,$id,$page ,$rows,$pic_name  );
		echo $data;
	}

	function list_owner(){
		$page = $this->input->post('page');
		$rows = $this->input->post('rows');
		$id = $this->input->post('id');
		$option = $this->input->post('option');
		$owner_nama = $this->input->post('owner_nama');
		$data = $this->m_setup->list_owner($option,$id,$page ,$rows , $owner_nama);
		echo $data;
	}

	function delete(){
 		$tipe = $this->input->post("tab");
		if ($tipe == "pic"){
			$data = array(
				"pic_id" => $this->input->post("id")
			);
			$this->m_setup->delete($data,$tipe);
		}else{
			$data = array(
				"owner_id" => $this->input->post("id")
			);
			$this->m_setup->delete($data,$tipe);
		}
	}
	
	function save(){
 		$tipe = $this->input->post("tipe");
		if ($tipe == "pic"){
			$id = $this->input->post("txt-stafid");
			$data = array(
				"pic_name" => $this->input->post("txt-nama") , 
				"pic_active" => $this->input->post("check-aktif") ,
			);
			echo $this->m_setup->save($id,$data,$tipe);
		}else{
			$id = $this->input->post("txt-ownerid");
			$data = array(
				"owner_nama" => strtoupper( $this->input->post("txt-ownernama") ), 
				"owner_jenis" => $this->input->post("cmb-jenis") ,
				"owner_aktif" => $this->input->post("check-owneraktif") ,
			);
			echo $this->m_setup->save($id,$data,$tipe);
		}	
	}
	
	function update(){
		if ($this->input->post("txt-merah") != ""){
			$data_where = array( "param1"  => "MERAH");
			$data_submit = array( "nvalue1"  => $this->input->post("txt-merah"));
			$this->m_setup->update($data_where,$data_submit);
		}
		
		if ($this->input->post("txt-kuning") != ""){
			$data_where = array( "param1"  => "KUNING");
			$data_submit = array( "nvalue1"  => $this->input->post("txt-kuning"));
			$this->m_setup->update($data_where,$data_submit);
		}
		
		if ($this->input->post("txt-hijau") != ""){
			$data_where = array( "param1"  => "HIJAU");
			$data_submit = array( "nvalue1"  => $this->input->post("txt-hijau"));
			$this->m_setup->update($data_where,$data_submit);
		}
		
		if ($this->input->post("txt-refresh") != ""){
			$data_where = array( "param1"  => "REFRESH");
			$data_submit = array( "nvalue1"  => $this->input->post("txt-refresh"));
			$this->m_setup->update($data_where,$data_submit);
		}
	}
	
	function ubah_password(){
		$data = array(
			"user_passwd" => md5($this->input->post("password"))
		);
		$this->m_setup->ubah_password($data);
	}
}